package cn.cpf.app.pane;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 整数进制转换器
 * @author by CPF
 */
public class RadixConversion extends JFrame {
	private static final long serialVersionUID = -4791222140191948495L;

	private final JPanel contentPane;
	private final JTextField RadixTextField;
	private final JTextField ConvertTextField;
	private static final String[] scale = {"Binary","Octal","D10","Hex"};
	private final int [] scaleNum = {2,8,10,16};
	private int radixCombo, convertCombo, conversingNum;
	private String ConvertStrNum;
	private boolean exceedFlag;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(() -> {
			try {
				RadixConversion frame = new RadixConversion();
				frame.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RadixConversion() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 325, 203);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		RadixTextField = new JTextField();
		RadixTextField.setBounds(31, 35, 92, 21);
		panel.add(RadixTextField);
		RadixTextField.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("\u6240\u8F6C\u6362\u6570");
		lblNewLabel.setBounds(180, 10, 116, 15);
		panel.add(lblNewLabel);
		
		JLabel radix = new JLabel("\u8BF7\u8F93\u5165\u8981\u8F6C\u6362\u7684\u57FA\u6570");
		radix.setLabelFor(RadixTextField);
		radix.setBounds(21, 10, 116, 15);
		panel.add(radix);
		
		JLabel lblNewLabel_1 = new JLabel("\u8BF7\u9009\u62E9\u57FA\u6570\u7684\u7C7B\u578B");
		lblNewLabel_1.setBounds(21, 66, 116, 15);
		panel.add(lblNewLabel_1);
		
		JComboBox radixComboBox = new JComboBox(scale);
		lblNewLabel_1.setLabelFor(radixComboBox);
		radixComboBox.setBounds(31, 91, 92, 21);
		panel.add(radixComboBox);
		
		ConvertTextField = new JTextField();
		lblNewLabel.setLabelFor(ConvertTextField);
		ConvertTextField.setBounds(180, 35, 92, 21);
		panel.add(ConvertTextField);
		ConvertTextField.setColumns(10);
		
		JComboBox converComboBox = new JComboBox(scale);
		converComboBox.setBounds(180, 91, 92, 21);
		panel.add(converComboBox);
		
		JLabel lblNewLabel_2 = new JLabel("\u8BF7\u9009\u62E9\u6240\u8F6C\u6362\u7684\u7C7B\u578B");
		lblNewLabel_2.setLabelFor(lblNewLabel_2);
		lblNewLabel_2.setBounds(180, 66, 116, 15);
		panel.add(lblNewLabel_2);
		
		JButton ConverBtn = new JButton("Conver");
		ConverBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exceedFlag = false;
				radixCombo = radixComboBox.getSelectedIndex();
				convertCombo = converComboBox.getSelectedIndex();
				//将需要转换的基数转换为 十进制 整数。
				ConvertTextField.setForeground(Color.black);
				try {
					conversingNum = Integer.parseInt(RadixTextField.getText(),scaleNum[radixCombo]);
				} catch (NumberFormatException e) {
					// TODO 自动生成的 catch 块
//					e.printStackTrace();
					ConvertTextField.setText("error");
					ConvertTextField.setForeground(Color.red);
					exceedFlag = true;
				}
				//将 十进制 整数转换为所需进制的String类型
				if(!exceedFlag){
				ConvertStrNum = Integer.toString(conversingNum, scaleNum[convertCombo]);
				ConvertTextField.setText(ConvertStrNum);}
			}
		});
		ConverBtn.setBounds(107, 122, 92, 21);
		panel.add(ConverBtn);
	}
}
